package cn.lingyangwl.agile.pay.mch.controller;

import cn.lingyangwl.framework.core.response.Resp;
import cn.lingyangwl.framework.tool.core.exception.Assert;
import cn.lingyangwl.framework.tool.core.exception.BizException;
import cn.lingyangwl.agile.common.core.exception.enums.GlobalErrorEnum;
import cn.lingyangwl.agile.pay.model.rqrs.MchInfoQL;
import cn.lingyangwl.agile.pay.model.rqrs.MchInfoRQ;
import cn.lingyangwl.agile.pay.model.rqrs.MchInfoRS;
import cn.lingyangwl.agile.pay.common.service.MchInfoService;
import cn.lingyangwl.agile.ram.ActionType;
import cn.lingyangwl.agile.ram.RamResource;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author shenguangyang
 */
@Slf4j
@RestController
@RequestMapping("/mch/info")
public class MchInfoController {
    @Resource
    private MchInfoService mchInfoService;

    @RamResource(action = "pay:mchInfo:list", name = "查询商户列表", type = ActionType.READ)
    @GetMapping("/page")
    public Resp<IPage<MchInfoRS>> page(MchInfoQL query) {
        IPage<MchInfoRS> page = mchInfoService.pageMch(query);
        return Resp.ok(page);
    }

    @RamResource(action = "pay:mchInfo:save", name = "保存商户", type = ActionType.WRITE)
    @PostMapping("/save")
    public Resp<?> save(@RequestBody MchInfoRQ req) {
        mchInfoService.saveMch(req);
        return Resp.ok();
    }

    @RamResource(action = "pay:mchInfo:delete", name = "删除商户", type = ActionType.WRITE)
    @DeleteMapping("/deleteById")
    public Resp<?> delete(@RequestParam("mchNo") String mchNo) {
        List<Long> userIds = mchInfoService.removeByMchNo(mchNo);
        return Resp.ok();
    }

    @RamResource(action = "pay:mchInfo:update", name = "更新商户", type = ActionType.WRITE)
    @DeleteMapping("/update")
    public Resp<?> update(@RequestBody MchInfoRQ req) {
        mchInfoService.updateMch(req);
        return Resp.ok();
    }

    @RamResource(action = "pay:mchInfo:get", name = "查询商户详情", type = ActionType.READ)
    @GetMapping("/getByMchNo")
    public Resp<MchInfoRS> getByMchNo(@RequestParam("mchNo") String mchNo) {
        MchInfoRS mchInfo = mchInfoService.getMchInfoByMchNo(mchNo);
        Assert.notNull(mchInfo, new BizException(GlobalErrorEnum.OPERATION_FAIL_SELETE));
        return Resp.ok(mchInfo);
    }
}
